package com.prp.struts.action;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.prp.dao.Userinfo;
import com.prp.dao.UserinfoDAO;
import com.prp.service.ServiceFactory;

public class UserManageAction extends ActionSupport {

	/**
	 * 
	 */
	
	private String searchInfo;
	
	private List userList;
	
	private int pageSize = 15;
	
	private int pageNow;
	
	private int pageCount;
	
	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageNow() {
		return pageNow;
	}

	public void setPageNow(int pageNow) {
		this.pageNow = pageNow;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public List getUserList() {
		return userList;
	}

	public void setUserList(List userList) {
		this.userList = userList;
	}

	public String getSearchInfo(){
		return this.searchInfo;
	}
	
	public void setSearchInfo(String searchInfo){
		this.searchInfo = searchInfo;
	}
	
	public String searchUser() {
		userList = ServiceFactory.getAdminSystemSer().searchUser(searchInfo);
				
		return SUCCESS;
	}
	
	public String userlist() throws UnsupportedEncodingException{
			
		if(pageNow <= 0)
			pageNow = 1;
		
		if(searchInfo == null){
			searchInfo = "";
			pageCount = ServiceFactory.getAdminSystemSer().getAllUser().size();
		}
		else{
			searchInfo = new String(searchInfo.getBytes("iso-8859-1"), "utf-8");
			
			pageCount = ServiceFactory.getAdminSystemSer().searchUser(searchInfo).size();
		}
		
		if(pageCount%pageSize == 0){
			if(pageNow > pageCount/pageSize)
				pageNow = pageCount / pageSize;
		}else{
			if(pageNow > pageCount/pageSize + 1)
				pageNow = pageCount / pageSize + 1;
		}
		

		
		userList = ServiceFactory.getAdminSystemSer().getAllUserByPage(searchInfo, pageSize, pageNow);
	
		return "userlist";
	}

}
